<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace cmf\controller;

use think\Controller;
use think\Request;
use think\View;
use think\Config;
use qcos\Cos;

class BaseController extends Controller
{
    /**
     * 构造函数
     * @param Request $request Request对象
     * @access public
     */
    public function __construct(Request $request = null)
    {

        if (is_null($request)) {
            $request = Request::instance();
        }

        $this->request = $request;

        $this->_initializeView();
        $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));


        // 控制器初始化
        $this->_initialize();

        // 前置操作方法
        if ($this->beforeActionList) {
            foreach ($this->beforeActionList as $method => $options) {
                is_numeric($method) ?
                    $this->beforeAction($options) :
                    $this->beforeAction($method, $options);
            }
        }
    }


    // 初始化视图配置
    protected function _initializeView()
    {
    }

    /**
     *  排序 排序字段为list_orders数组 POST 排序字段为：list_order
     */
    protected function listOrders($model)
    {
        if (!is_object($model)) {
            return false;
        }

        $pk  = $model->getPk(); //获取主键名称
        $ids = $this->request->post("list_orders/a");

        if (!empty($ids)) {
            foreach ($ids as $key => $r) {
                $data['list_order'] = $r;
                $model->where([$pk => $key])->update($data);
            }

        }

        return true;
    }


    public function upPath()
    {
        return date('Y/m/');
    }

    public function upName()
    {
        return (microtime (true)*10000);
    }

    public function moveImg($path,$file,$type)
    {
        $original = VERSION_LIVESTAR.'/Default/'.$path.$file;
        switch ($type){
            case 0:
                $address = VERSION_LIVESTAR.'/'.$path.$file;
                break;
            case 1:
                $address = VERSION_LIVESTAR.'/Users/'.$path.$file;
                break;
            case 2:
                $address = VERSION_LIVESTAR.'/Stars/'.$path.$file;
                break;
            case 3:
                $address = VERSION_LIVESTAR.'/Circles/'.$path.$file;
                break;
            case 4:
                $address = VERSION_LIVESTAR.'/Shout/'.$path.$file;
                break;
            case 5:
                $address = VERSION_LIVESTAR.'/Deduction/'.$path.$file;
                break;
            case 6:
                $address = VERSION_LIVESTAR.'/Advert/'.$path.$file;
                break;
            case 7:
                $address = VERSION_LIVESTAR.'/APK/'.$path.$file;
                break;
            default:
                $address = VERSION_LIVESTAR.'/Default/'.$path.$file;
                break;
        }
        $cos = new Cos();
        $cos->moveFile($original, '/'.$address);
        return HTTP_IMG.$address;
    }

    public function deleteImg($pathFile)
    {
        $cos = new Cos();
        $del = $cos->delFile($pathFile);
        if($del['code']){
            $msg['message'] = '图片删除错误:'.$del['message'];
        }else{
            $msg['message'] = '图片删除成功';
        }
        return $msg;
    }
    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    public function httpPost($url,$param,$post_file=false){
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach($param as $key=>$val){
                $aPOST[] = $key."=".urlencode($val);
            }
            $strPOST =  join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($oCurl, CURLOPT_POST,true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            echo $sContent;
        }else{
            echo $sContent;
        }
    }

}